﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SlimDX;
using Gype.UI;

namespace Gype.GUI.Components
{
    public class MainTab : Component
    {
        public MainTab()
        {
        }

        internal override void OnMessage(ControlMessage message)
        {
            if (message is MouseMessage)
            {
                MouseMessage msg = message as MouseMessage;
                switch (msg.Message)
                {
                    case Win32Native.WM.MOUSEMOVE:
                        {
                            mTabHover = GetItemIndex(msg.X, msg.Y);
                        }
                        break;

                    case Win32Native.WM.LBUTTONDOWN:
                    case Win32Native.WM.NCLBUTTONDBLCLK:
                        {
                            mTabDown = GetItemIndex(msg.X, msg.Y);
                        }
                        break;

                    case Win32Native.WM.LBUTTONUP:
                        {
                            int itemUp = GetItemIndex(msg.X, msg.Y);
                            if (itemUp == mTabDown && itemUp != -1)
                                mSelectedTab = itemUp;

                            mTabDown = -1;
                        }
                        break;
                }
            }

            foreach (TabPage page in mPages)
            {
                message.ForwardMessage(this, page);
            }
        }

        internal override void RenderObject()
        {
            if (mTarget == null)
                return;

            mTarget.FillRectangle(Vector2.Zero, new Vector2(300, 25), BrushCollection.TabHeaderBackground);


            if (mPages.Count != 0)
            {
                float curTabOfs = 5;
                int i = 0;
                foreach (var page in mPages)
                {
                    float realTabSize = (float)Math.Max(page.TextWidth + 6, 50);
                    if (i == mSelectedTab)
                    {
                        mHeaderText.Color = BrushCollection.TabHeaderTextSelect;
                        mTarget.FillRectangle(new Vector2(curTabOfs + 1, 3), new Vector2(realTabSize - 1, 22), BrushCollection.TabHeaderItemBackgroundSelect);
                        mTarget.DrawRectangle(new Vector2(curTabOfs, 2), new Vector2(realTabSize, 23), BrushCollection.TabHeaderItemBorderSelect, 1, RectangleFillMask.NotBottom);
                        mTarget.DrawRectangle(new Vector2(0, 24), new Vector2(curTabOfs, 1), BrushCollection.TabHeaderItemBorderSelect, 1, RectangleFillMask.Top);
                        mTarget.DrawRectangle(new Vector2(curTabOfs + realTabSize, 24), new Vector2(300 - curTabOfs - realTabSize, 1), BrushCollection.TabHeaderItemBorderSelect, 1, RectangleFillMask.Top);
                    }
                    else
                    {
                        mHeaderText.Color = BrushCollection.TabHeaderText;
                        Brush backgBrush = BrushCollection.TabHeaderItemBackground;
                        if (i == mTabHover)
                            backgBrush = BrushCollection.TabHeaderItemBackgroundHover;

                        mTarget.FillRectangle(new Vector2(curTabOfs + 1, 3), new Vector2(realTabSize - 1, 21), backgBrush);
                        mTarget.DrawRectangle(new Vector2(curTabOfs, 2), new Vector2(realTabSize, 22), BrushCollection.TabHeaderItemBorder, 1, RectangleFillMask.NotBottom);
                    }

                    mHeaderText.DrawText(page.Header, new Vector2(curTabOfs + 2, 6), new Vector2(page.TextWidth + 1, 13));
                    curTabOfs += realTabSize;
                    if (i == mSelectedTab)
                        page.RenderObject();

                    ++i;
                }
            }
        }

        internal override void RenderTopLevel()
        {
            int i = 0;
            foreach (var page in mPages)
            {
                if (i == mSelectedTab)
                    page.RenderTopLevel();

                ++i;
            }
        }

        internal int GetItemIndex(float x, float y)
        {
            if (x < 5 || y < 2 || y > 25)
                return -1;

            float start = 5;
            int i = 0;
            foreach (var page in mPages)
            {
                if (x >= start && x <= (start + (float)Math.Max(page.TextWidth + 6, 50)))
                    return i;

                start += (float)Math.Max(page.TextWidth + 6, 50);
                ++i;
            }

            return -1;
        }

        public void AddTabPage(TabPage page)
        {
            page.TextWidth = mHeaderText.MeasureString(page.Header, new Vector2(500, 13)).Width;
            page.SetTabParent(this);
            mPages.Add(page);
        }

        internal void AttachToFrame(MainFrame frame)
        {
            mGUIManager = frame.mGUIManager;
            mFrame = frame;
            mTarget = mFrame.mGUIManager.Target;
            mHeaderText = new TextElement(TextCollection.Arial);
            mHeaderText.Alignment = TextAlignement.Left;

            TabPage contacts = new TabPage();
            contacts.Header = "Contact List";
            
            TabPage chats = new TabPage();
            chats.Header = "Conversations";

            AddTabPage(contacts);
            AddTabPage(chats);

            LoadContactList(contacts);
            LoadConvList(chats);
        }

        private void LoadContactList(TabPage contactsPage)
        {
            var pageSize = GetPageSizeFill();
            mContactList = new Components.ContactList();
            mContactList.Location = new System.Drawing.PointF(3, 3);
            mContactList.Size = new System.Drawing.SizeF(pageSize.X - 6, pageSize.Y - 6);
            contactsPage.addChild(mContactList);
        }

        private void LoadConvList(TabPage convPage)
        {
            var pageSize = GetPageSizeFill();
            mConvList = new ConvList();
            mConvList.Location = new System.Drawing.PointF(3, 3);
            mConvList.Size = new System.Drawing.SizeF(pageSize.X - 6, pageSize.Y - 6);
            convPage.addChild(mConvList);
        }

        internal Vector2 GetPageSizeFill()
        {
            var size = mFrame.getClientSize();
            size.X = 300;
            return new Vector2(size.X, size.Y - 25);
        }

        internal ContactList ContactList { get { return mContactList; } }
        internal ConvList Conversations { get { return mConvList; } }

        private ConvList mConvList;
        private ContactList mContactList;
        private int mTabDown = -1;
        private int mTabHover = -1;
        private int mSelectedTab = 0;
        private List<TabPage> mPages = new List<TabPage>();
        private MainFrame mFrame;
        internal Rendertarget mTarget;
        private TextElement mHeaderText;
    }
}
